﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ARR.UI.ViewModels;
using Telerik.Windows.Controls;
using ARR.Entities;

namespace ARR.UI.Views
{
    /// <summary>
    /// Interaction logic for CategoryMatchMasterView.xaml
    /// </summary>
    public partial class CategoryMatchMasterView : UserControl
    {
        public CategoryMatchMasterView()
        {
            InitializeComponent();

            NACSCategoryMatchGV.PreparingCellForEdit += new EventHandler<Telerik.Windows.Controls.GridViewPreparingCellForEditEventArgs>(NACSCategoryMatchGV_PreparingCellForEdit);
            OtherCategoryMatchGV.PreparingCellForEdit += new EventHandler<Telerik.Windows.Controls.GridViewPreparingCellForEditEventArgs>(OtherCategoryMatchGV_PreparingCellForEdit);

        }
        
        void NACSCategoryMatchGV_PreparingCellForEdit(object sender, Telerik.Windows.Controls.GridViewPreparingCellForEditEventArgs e)
        {
            if (e.Column.UniqueName == "SourceCategory")
            {
                CategoryMatchMasterViewModel model = this.DataContext as CategoryMatchMasterViewModel;
                sourceCategoryRadDropDownButton = e.EditingElement as RadDropDownButton;
                RadTreeView treeview = sourceCategoryRadDropDownButton.DropDownContent as RadTreeView;
                if (treeview != null)
                {
                    treeview.ItemsSource = model.Categories;
                    sourceCategoryItem = e.Row.DataContext;
                }
            }

            if (e.Column.UniqueName == "TargetCategory")
            {
                CategoryMatchMasterViewModel model = this.DataContext as CategoryMatchMasterViewModel;
                targetCategoryRadDropDownButton = e.EditingElement as RadDropDownButton;
                RadTreeView treeview = targetCategoryRadDropDownButton.DropDownContent as RadTreeView;
                if (treeview != null)
                {
                    treeview.ItemsSource = model.Categories;
                    targetCategoryItem = e.Row.DataContext;
                }
            }
        }

        void OtherCategoryMatchGV_PreparingCellForEdit(object sender, Telerik.Windows.Controls.GridViewPreparingCellForEditEventArgs e)
        {
            if (e.Column.UniqueName == "TargetCategory")
            {
                CategoryMatchMasterViewModel model = this.DataContext as CategoryMatchMasterViewModel;
                targetCategoryForNotNacsCategoryRadDropDownButton = e.EditingElement as RadDropDownButton;
                RadTreeView treeview = targetCategoryForNotNacsCategoryRadDropDownButton.DropDownContent as RadTreeView;
                if (treeview != null)
                {
                    treeview.ItemsSource = model.Categories;
                    targetCategoryForNotNacsCategoryItem = e.Row.DataContext;
                }
            }
        }


        private object sourceCategoryItem;
        private RadDropDownButton sourceCategoryRadDropDownButton;
        private void SourceCategory_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RadTreeView treeview = sender as RadTreeView;

            if (treeview.SelectedItem != null)
            {
                if (((Category)treeview.SelectedItem).Level == 2)
                {
                    MessageBox.Show("Los SKUs sólo pueden ser asociados a una subcategoría");
                }
                else
                {
                    if (treeview != null && treeview.SelectedItem != null)
                    {
                        if (sourceCategoryItem != null)
                        {
                            ((CategoryMatch)sourceCategoryItem).SourceCategory = ((Category)treeview.SelectedItem);
                            ((CategoryMatch)sourceCategoryItem).SourceCategoryId = ((Category)treeview.SelectedItem).Id;
                            if (sourceCategoryRadDropDownButton != null)
                            {
                                sourceCategoryRadDropDownButton.IsOpen = false;
                                this.NACSCategoryMatchGV.CommitEdit();
                            }
                        }
                    }
                }
            }
        }

        private object targetCategoryItem;
        private RadDropDownButton targetCategoryRadDropDownButton;
        private void TargetCategory_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RadTreeView treeview = sender as RadTreeView;

            if (treeview.SelectedItem != null)
            {
                if (((Category)treeview.SelectedItem).Level == 2)
                {
                    MessageBox.Show("Los SKUs sólo pueden ser asociados a una subcategoría");
                }
                else
                {
                    if (treeview != null && treeview.SelectedItem != null)
                    {
                        if (targetCategoryItem != null)
                        {
                            ((CategoryMatch)targetCategoryItem).TargetCategory = ((Category)treeview.SelectedItem);
                            ((CategoryMatch)targetCategoryItem).CategoryId = ((Category)treeview.SelectedItem).Id;
                            if (targetCategoryRadDropDownButton != null)
                            {
                                targetCategoryRadDropDownButton.IsOpen = false;
                                this.NACSCategoryMatchGV.CommitEdit();
                            }
                        }
                    }
                }
            }
        }


        private object targetCategoryForNotNacsCategoryItem;
        private RadDropDownButton targetCategoryForNotNacsCategoryRadDropDownButton;
        private void TargetCategoryForNotNacsCategory_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RadTreeView treeview = sender as RadTreeView;

            if (treeview.SelectedItem != null)
            {
                if (((Category)treeview.SelectedItem).Level == 2)
                {
                    MessageBox.Show("Los SKUs sólo pueden ser asociados a una subcategoría");
                }
                else
                {
                    if (treeview != null && treeview.SelectedItem != null)
                    {
                        if (targetCategoryForNotNacsCategoryItem != null)
                        {
                            ((CategoryMatch)targetCategoryForNotNacsCategoryItem).TargetCategory = ((Category)treeview.SelectedItem);
                            ((CategoryMatch)targetCategoryForNotNacsCategoryItem).CategoryId = ((Category)treeview.SelectedItem).Id;
                            if (targetCategoryForNotNacsCategoryRadDropDownButton != null)
                            {
                                targetCategoryForNotNacsCategoryRadDropDownButton.IsOpen = false;
                                this.NACSCategoryMatchGV.CommitEdit();
                            }
                        }
                    }
                }
            }
        }
    }
}
